package com.jml.三期.ConcurrentHashMap7;

import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;

public class MyConcurrentHashMap<K,V> {
    private Hashtable<K, V>[] segments;

    public MyConcurrentHashMap() {
        segments = new Hashtable[16];
    }

    public void put(K k, V v) {
        int segmentIndex = k.hashCode() & segments.length - 1;
        Hashtable hashtable = segments[segmentIndex];
        if (hashtable == null) {
            hashtable = new Hashtable<K, V>();
        }
        hashtable.put(k, v);
        segments[segmentIndex] = hashtable;
    }

    public V get(K k) {
        int segmentIndex = k.hashCode() & segments.length - 1;
        Hashtable<K, V> hashtable = segments[segmentIndex];
        if (hashtable != null) {
            return hashtable.get(k);
        }
        return null;
    }

    public static void main(String[] args) {
        MyConcurrentHashMap<String, String> concurrentHashMap = new MyConcurrentHashMap<>();
//        concurrentHashMap.put("a", "a");
//        concurrentHashMap.put("97", "97");
        for (int i = 0; i < 10; i++) {
            concurrentHashMap.put(i + "", i + "");
        }
//        System.out.println(concurrentHashMap.get("97"));
        for (int i = 0; i < 10; i++) {
            System.out.println(concurrentHashMap.get(i + ""));
        }
        ConcurrentHashMap<Object, Object> a = new ConcurrentHashMap<>();
        a.size();


    }
}
